﻿<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" >
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>栏目 - 烫烫博客管理</title>
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">
<link rel="stylesheet" type="text/css" href="/css/font-awesome.min.css">
<link rel="apple-touch-icon-precomposed" href="/img/icon/icon.png">
<link rel="Shortcut Icon" href="favicon.ico" type="image/ico">
<script src="js/jquery-2.1.4.min.js"></script>
<!--[if gte IE 9]>
  <script src="/js/jquery-1.11.1.min.js" type="text/javascript"></script>
  <script src="/js/html5shiv.min.js" type="text/javascript"></script>
  <script src="/js/respond.min.js" type="text/javascript"></script>
  <script src="/js/selectivizr-min.js" type="text/javascript"></script>
<![endif]-->
<!--[if lt IE 9]>
  <script>window.location.href='upgrade-browser.html';</script>
<![endif]-->
</head>

<body class="user-select">
<section class="container-fluid">
  <header>
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">切换导航</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
          <a class="navbar-brand" href="/">烫烫</a> </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="/logout" onClick="if(!confirm('是否确认退出？'))return false;">退出登录</a></li>
          </ul>
          <form action="" method="post" class="navbar-form navbar-right" role="search">
            <div class="input-group">
              <input type="text" class="form-control" autocomplete="off" placeholder="键入关键字搜索" maxlength="15">
              <span class="input-group-btn">
              <button class="btn btn-default" type="submit">搜索</button>
              </span> </div>
          </form>
        </div>
      </div>
    </nav>
  </header>
  <div class="row">
    <aside class="col-sm-3 col-md-2 col-lg-2 sidebar">
      <ul class="nav nav-sidebar">
        <li><a href="/adminindex.html">关于</a></li>
      </ul>
      <ul class="nav nav-sidebar">
        <li ><a href="/show-article.html">文章</a></li>
      </ul>
      <ul class="nav nav-sidebar">
        <li ><a href="/category.html">分类</a></li>
      </ul>
      <ul class="nav nav-sidebar">
        <li class="active"><a href="/tag.html">标签</a></li>
      </ul>
      <ul class="nav nav-sidebar">
        <li><a href="/link.html">友链</a></li>
      </ul>

    </aside>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-lg-10 col-md-offset-2 main" id="main">
      <div class="row">
        <div class="col-md-5">
          <h1 class="page-header">添加</h1>
          <form autocomplete="off">
            <div class="form-group">
              <label for="tagname">标签名称</label>
              <input type="text" id="tagname" name="tagname" class="form-control" placeholder="在此处输入标签名称" required autocomplete="off">
              <span class="prompt-text">这将是它在站点上显示的名字。</span> </div>
            <div class="form-group">
              <label for="tagalias">标签别名</label>
              <input type="text" id="tagalias" name="tagalias" class="form-control" placeholder="在此处输入栏目别名" required autocomplete="off">
              <span class="prompt-text">“别名”是在URL中使用的别称，它可以令URL更美观。通常使用小写，只能包含字母，数字和连字符（-）。</span> </div>

            <button class="btn btn-primary" type="button" id="save">添加新标签</button>
          </form>
        </div>
        <form class="col-md-7">
          <h1 class="page-header">管理 <span class="badge"></span></h1>
          <input style="display: none" id="refresh" type="button" value="刷新">
          <div class="table-responsive">
            <table class="table table-striped table-hover">
              <thead>
                <tr>
                  <th><span class="glyphicon glyphicon-paperclip"></span> <span class="visible-lg">ID</span></th>
                  <th><span class="glyphicon glyphicon-file"></span> <span class="visible-lg">名称</span></th>
                  <th><span class="glyphicon glyphicon-list-alt"></span> <span class="visible-lg">别名</span></th>
                  <th><span class="glyphicon glyphicon-pushpin"></span> <span class="visible-lg">总数</span></th>
                  <th><span class="glyphicon glyphicon-pencil"></span> <span class="visible-lg">操作</span></th>
                </tr>
              </thead>
              <tbody id="tbody-result">

              </tbody>
            </table>
          </div>
        </form>
      </div>
    </div>
  </div>
</section>

<script src="/js/bootstrap.min.js"></script>
<script src="/js/admin-scripts.js"></script>
<script>
//是否确认删除
function delete_tag(id) {
    if (window.confirm("此操作不可逆，是否确认？"))
        $.ajax({
            type: "POST",
            url: "/tag/delete",
            data: {"id": id},
            cache: false, //不缓存此页面
            success: function (data) {
                if (data == "success") {
                    tr = document.getElementById(id);
                    tr.style.display = "none";
                } else {
                    alert("删除失败")
                }
            },
            error: function () {
                alert("删除error")
            }
        });
}

//获取所有标签
$(function(){
    $("#refresh").click(function() {
        $.ajax({
            url : "/tag/selectAllTagsWithArticles",
            type : "POST",
            dataType: 'json',
            data: { },
            success:function(data) {
                <!-- 处理后端返回的数据 -->
                var str = "";
                var tbody=window.document.getElementById("tbody-result");
                for (i in data) {


                    str += "<tr id='"+data[i].tagid+"'>" +
                        "<td>"+data[i].tagid+"</td>"+
                        "<td>"+data[i].tagname+"</td>"+
                        "<td>"+data[i].tagalias+"</td>"+
                        "<td>"+data[i].articles.length+"</td>"+
                        "<td><a href=\"update-tag.html?id="+data[i].tagid+"\">修改</a>";
                    if(data[i].articles.length==0)
                        str+="<a onclick='delete_tag("+data[i].tagid+")'>&emsp;删除</a>";

                     str+="</td></tr>";
                }
                tbody.innerHTML = str;
            },
            error: function (data) {
                  alert("刷新失败")
            }
        });
    });
});

$(document).ready(function(e) {
    $('#refresh').click();
});


$(function(){
    $("#save").click(function() {
        $.ajax({
            url : "/tag/saveTag",
            type : "POST",
            dataType:"json",
            contentType : "application/json;charset=UTF-8",
            <!-- 向后端传输的数据 -->
            data : JSON.stringify({
                tagname :  $("#tagname").val(),
                tagalias:  $("#tagalias").val()
            }),
            success:function(data) {
                <!-- 处理后端返回的数据 -->
                if(data==1){
                    document.getElementById("tagname").value="";
                    document.getElementById("tagalias").value="";
                    $('#refresh').click();
                }else{
                    window.alert("添加失败")
                }

            },
            error:function(data){
                window.alert("添加error")
            }
        });
    });
});
</script>
</body>
</html>
